var StartLayer = cc.Layer.extend({
    isMouseDown:false,

    ccbNode:null,
    sceneStatus:0,
    recordRect1:null,
    recordRect2:null,
    recordRect3:null,
    delRect1:null,
    delRect2:null,
    delRect3:null,
    init:function () {

        //////////////////////////////
        // 1. super init first
        this._super();
        this.setTouchEnabled(true);
        /////////////////////////////
        // 2. add a menu item with "X" image, which is clicked to quit the program
        //    you may modify it.
        // ask director the window size
        var size = cc.Director.getInstance().getVisibleSize();
        var size2 =  cc.Director.getInstance().getWinSize();

        cc.BuilderReader.setResourcePath("ccb/");

        //load ccbi scene
        this.ccbNode = cc.BuilderReader.load("ccb/StartScene.ccbi", this);
        this.ccbNode.animationManager.runAnimationsForSequenceNamed("start");
        this.ccbNode.animationManager.setCompletedAnimationCallback(this, this.animationCompleteCallback);

        //init scene
        //read record
        /*
            tap_start 1
            start_scroll 20 21 22
                box 1-4 
                    character 1
                lv 5 
                time 6
            delete 10
        */

        for(var i=1;i<=3;i++){
            if(Game.gameRecord[i]["have_record"]==0){
                // 没有记录
                var lvLabel=this.ccbNode.getChildByTag(19+i).getChildByTag(5);
                var timeLabel=this.ccbNode.getChildByTag(19+i).getChildByTag(6);
                lvLabel.setString("No Data");
                timeLabel.setString("No Data");
                for(var j=1;j<=4;j++){
                    var characterSprite=this.ccbNode.getChildByTag(19+i).getChildByTag(j).getChildByTag(1).setVisible(false);
                }
            }else{
                var lvLabel=this.ccbNode.getChildByTag(19+i).getChildByTag(5);
                var timeLabel=this.ccbNode.getChildByTag(19+i).getChildByTag(6);
                lvLabel.setString("Lv "+Game.gameRecord[i]["player"]["lv"]);
                timeLabel.setString(Game.gameRecord[i]["player"]["name"]);
                for(var j=1;j<=4;j++){
                    var characterSprite=this.ccbNode.getChildByTag(19+i).getChildByTag(j).getChildByTag(1);
                    var recordDetail=Game.gameRecord[i]["character"][0];
                    if(recordDetail[j]>0){
                        var texture = cc.TextureCache.getInstance().addImage("character/"+recordDetail[j]+".png");
                        characterSprite.setTexture(texture);
                    }else{
                        characterSprite.setVisible(false);
                    }
                }
            }
        }

        if(this.ccbNode != null) {
            this.addChild(this.ccbNode);
        }
    },
    animationCompleteCallback: function(){
        console.log("animation end...");
        if(this.sceneStatus==1){
            this.initTouchPositions();
        }
    },
    initTouchPositions: function(){
        console.log("init Touch Positions");
        if(this.recordRect1==null){
            var record1=this.ccbNode.getChildByTag(20);
            this.recordRect1=record1.getBoundingBox();
        }
        if(this.recordRect2==null){
            var record2=this.ccbNode.getChildByTag(21);
            this.recordRect2=record2.getBoundingBox();
        }
        if(this.recordRect3==null){
            var del1=this.ccbNode.getChildByTag(22);
            this.recordRect3=del1.getBoundingBox();
        }
        if(this.delRect1==null){
            var del1=this.ccbNode.getChildByTag(10);
            this.delRect1=del1.getBoundingBox();
        }
        if(this.delRect2==null){
            var del2=this.ccbNode.getChildByTag(11);
            this.delRect2=del2.getBoundingBox();
        }
        if(this.delRect3==null){
            var del3=this.ccbNode.getChildByTag(12);
            this.delRect3=del3.getBoundingBox();
        }
    },
    onTouchesEnded: function(pTouch, pEvent){
        var touchPoint=pTouch[0].getLocation();
        console.log("touch end",touchPoint);

        if(this.sceneStatus==0){
            console.log("touch start ! ! !");
            this.ccbNode.animationManager.runAnimationsForSequenceNamed("select_character");
            this.sceneStatus=1;
        }
        // if touch on any record or delete
        if(cc.rectContainsPoint(this.recordRect1, touchPoint)){
            console.log("R E C O R D 1");
            this.enterGame(1);
        }
        if(cc.rectContainsPoint(this.recordRect2, touchPoint)){
            console.log("R E C O R D 2");
            this.enterGame(2);
        }
        if(cc.rectContainsPoint(this.recordRect3, touchPoint)){
            console.log("R E C O R D 3");
            this.enterGame(3);
        }
        if(cc.rectContainsPoint(this.delRect1, touchPoint)){
            console.log("D E L 1");
            this.deleteGame(1);
        }
        if(cc.rectContainsPoint(this.delRect2, touchPoint)){
            console.log("D E L 2");
            this.deleteGame(2);
        }
        if(cc.rectContainsPoint(this.delRect3, touchPoint)){
            console.log("D E L 3");
            this.deleteGame(3);
        }
    },
    enterGame: function(id){
        if(Game.gameRecord[id]["have_record"]==0){
            Game.createRecord(id);
        }
        Game.currRecord=id;
        Game.randomShop();
        Game.currScene=new MainStageScene();
        cc.Director.getInstance().replaceScene(cc.TransitionFade.create(0.5, Game.currScene, cc.black()));
    },
    deleteGame: function(id){
        var lvLabel=this.ccbNode.getChildByTag(19+id).getChildByTag(5);
        var timeLabel=this.ccbNode.getChildByTag(19+id).getChildByTag(6);
        lvLabel.setString("No Data");
        timeLabel.setString("No Data");
        for(var j=1;j<=4;j++){
            var characterSprite=this.ccbNode.getChildByTag(19+id).getChildByTag(j).getChildByTag(1).setVisible(false);
        }
        Game.deleteRecord(id);
    },
    onTouchesBegan: function(pTouch, pEvent){
        var touchPoint=pTouch[0].getLocation();
    },
    onTouchesMoved: function(pTouch, pEvent){
        var touchPoint=pTouch[0].getLocation();
    },
});

var StartScene = cc.Scene.extend({
    onEnter:function () {
        this._super();
        var layer = new StartLayer();
        this.addChild(layer);
        layer.init();
    }
});